﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.App.Input.Bean;
using Shpm.System.Web.Control;

public partial class App_Commons_TeacherSearchForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(App_Commons_TeacherSearchForm));

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SysUserManager manger = new SysUserManager(Context);
            SysUserBean sysUserBean = new SysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            //检索标志位
            this.ViewState["SEARCH_FLG"] = 0;

            initPageControls();

            //传递检索参数与页面编号
            if (Context.Items.Contains("TeacherID"))
            {
                this.ViewState["TeacherID"] = Convert.ToString(this.Context.Items["TeacherID"]);
                this.txtId.Text = Convert.ToString(this.ViewState["TeacherID"]);
            }
            if (Context.Items.Contains("TEACHER_Name"))
            {
                this.ViewState["TEACHER_Name"] = Convert.ToString(this.Context.Items["TEACHER_Name"]);
                this.txtName.Text = Convert.ToString(this.ViewState["TEACHER_Name"]);
            }
            if (Context.Items.Contains("Sex"))
            {
                this.ViewState["Sex"] = Convert.ToString(this.Context.Items["Sex"]);
                this.sex.SelectedValue = Convert.ToString(this.ViewState["Sex"]);
            }
            if (Context.Items.Contains("XueLi"))
            {
                this.ViewState["XueLi"] = Convert.ToString(this.Context.Items["XueLi"]);
                this.XueLi.Text = Convert.ToString(this.ViewState["XueLi"]);
            }
            if (Context.Items.Contains("ParamPageIndex"))
            {
                this.ViewState["ParamPageIndex"] = Convert.ToInt32(this.Context.Items["ParamPageIndex"]);
                this.viewSrhRst.PageIndex = Convert.ToInt32(this.ViewState["ParamPageIndex"]);
            }
        }
    }

    private void initPageControls()
    {
        TeacherDao dao = new TeacherDao();
        CommonDictionaryBean bean = new CommonDictionaryBean();

        //数据词典加载
        bean.Catalog = "SEX";
        IList list = dao.FindByCatalog(bean);
        list.RemoveAt(0);
        list.Insert(0, new CommonDictionaryBean());
        sex.DataSource = list;
        sex.DataBind();
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        if ("1".Equals(Convert.ToString(this.ViewState["SEARCH_FLG"])))
        {
            return;
        }

        TeacherDao dao = new TeacherDao();
        TeacherBean bean = new TeacherBean();
        IList list = null;

        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["TeacherID"])))
        {
            bean.TeacherId = Convert.ToInt32(this.ViewState["TeacherID"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["TEACHER_Name"])))
        {
            bean.TeacherName = Convert.ToString(this.ViewState["TEACHER_Name"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["Sex"])))
        {
            bean.Sex = Convert.ToString(this.ViewState["Sex"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["XueLi"])))
        {
            bean.TeacherXueli = Convert.ToString(this.ViewState["XueLi"]);
        }

        //数据检索
        list = dao.GetList(bean);

        if (list == null || list.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
        }

        viewSrhRst.DataSource = list;
        viewSrhRst.DataBind();
    }



    protected void btnSeach_Click(object sender, EventArgs e)
    {
        ComValidateChecker validator = new ComValidateChecker();

        //数据校验
        validator.Add(new CheckLong(txtId.Text, new ComMessage("E0036", "教师编号")));
        validator.Add(new CheckMaxCharLen(txtName.Text, 10, new ComMessage("E0321", new String[] { "教师姓名", "10" })));

        //校验不通过
        if (!validator.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
            this.ViewState["SEARCH_FLG"] = 1;
            return;
        }

        //校验通过
        this.ViewState["SEARCH_FLG"] = 0;
        this.ViewState["TeacherID"] = txtId.Text;
        this.ViewState["TEACHER_Name"] = txtName.Text;
        this.ViewState["Sex"] = sex.SelectedValue;
        this.ViewState["XueLi"] = XueLi.Text;
    }

   

    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //翻页
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TeacherBean bean = (TeacherBean)e.Row.DataItem;
            (e.Row.FindControl("selectImg") as ImageButton).Attributes.Add("onclick", "selectTeacher('" + bean.TeacherId + "','" + bean.TeacherName + "')");
        }
    }
}
